const express = require('express');
const router = express.Router();
const { authMiddleware, validateRequest} = require('../tool/middleWare');
const asyncHandler = require('../tool/middleWare').asyncHandler;
const {
    changeWorkStatus,
    changeServicePrice,
    changeServiceStatus,
    createService,
    getAdvisorDetail
} = require('../service/advisor');

/**
 * 顾问更改工作状态
 */
router.get(
    '/work/status/change',
    authMiddleware,
    validateRequest({
        query: {
            status: {type: 'number', required: true, min: 0, max: 1},
        }
    }),
    asyncHandler(async (req, res) => {
        const { status } = req.query;
        const advisorId = req.loginUser.id;

        await changeWorkStatus(advisorId, Number(status));

        res.success(null, '工作状态已更新');
    })
);

/**
 * 顾问更改服务价格
 */
router.get(
    '/service/price/change',
    authMiddleware,
    validateRequest({
        query: {
            serviceId: {type: 'number', required: true},
            price: {type: 'number', required: true}
        }
    }),
    asyncHandler(async (req, res) => {
        const { serviceId, price } = req.query;
        const advisorId = req.loginUser.id;

        await changeServicePrice(advisorId, serviceId, price);

        res.success(null, '服务价格已更新');
    })
);

/**
 * 顾问修改服务状态
 */
router.get(
    '/service/status/change',
    authMiddleware,
    validateRequest({
        query: {
            serviceStatus: {type: 'number', required: true, min: 0, max: 1}
        }
    }),
    asyncHandler(async (req, res) => {
        const { serviceStatus } = req.query;
        const advisorId = req.loginUser.id;

        await changeServiceStatus(advisorId, Number(serviceStatus));

        res.success(null, '服务状态已更新');
    })
);

/**
 * 顾问新增服务
 */
router.post(
    '/service/create',
    authMiddleware,
    validateRequest({
        body: {
            serviceType: {type: 'number', required: true, min: 1, max: 4},
            price: {type: 'number', required: true},
            serviceName: {type: 'string', required: true}
        }
    }),
    asyncHandler(async (req, res) => {
        const { serviceType, price, serviceName } = req.body;
        const advisorId = req.loginUser.id;

        const data = await createService(advisorId, serviceType, price, serviceName);

        res.success(data, '服务已创建');
    })
);

/**
 * 顾问信息查询（详情）
 */
router.get(
    '/detail',
    authMiddleware,
    asyncHandler(async (req, res) => {
        const advisorId = req.loginUser.id;
        const data = await getAdvisorDetail(advisorId);
        res.success(data, '顾问详情获取成功');
    })
);

module.exports = router;